<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box ul {
            list-style: none;
            width: 60%;
            margin: auto;
            border: 1px solid black;
            padding: 10px;
        }

        #box ul li {
            display: flex;
            justify-content: space-around;
            margin-top: 10px;
            border-bottom: 1px dashed #cccccc;
        }

        #box ul li:last-child {
            border-bottom: none;
        }

        #box ul li img {
            width: 100px;
        }

        .all {
            width: 60%;
            margin: auto;
        }
    </style>
</head>

<body>
    <div id="box">
        <div class="all" v-if="list.length === 0">购物车空空如也</div>
        <template v-else>
            <div class="all">
                <!-- <input type="checkbox" v-model="isChecked" @change="allCheck()"> -->
                <input type="checkbox" v-model="computedCheckAll">
                全选|全不选
            </div>
            <ul>
                <li v-for="(item,index) in list" :key="item.id">
                    <div>
                        <!-- 将事件商品对象item绑定到value中，当选中当前商品时，item作为元素保存在checkGroup数组中，方便后面计算时使用 -->
                        <input type="checkbox" v-model="checkGroup" :value="item" @change="isAllCheck()" />
                    </div>
                    <div>
                        <img :src="item.pic" alt="">
                    </div>
                    <div>
                        <div>名称:{{item.name}}</div>
                        <div>价格：￥{{item.price}}</div>
                    </div>
                    <div>
                        <button @click="item.number--" :disabled="item.number===1">-</button>
                        <span>{{item.number}}</span>
                        <button @click="item.number++" :disabled="item.number===item.limit">+</button>
                    </div>
                    <div>
                        <button @click="deleteGood(index,item.id)">删除</button>
                    </div>
                </li>
            </ul>
            <div class="all">总金额: {{totalPrice()}}</div>
        </template>
    </div>
    <script src='./js/vue.js'></script>
    <script>
        var vm = new Vue({
            el: "#box",
            data: {
                isChecked: false,
                checkGroup: [],
                list: [
                    {
                        name: "商品1",
                        price: 10,
                        number: 1,
                        id: 1,
                        limit: 5,//限购
                        pic: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name: "商品2",
                        price: 20,
                        number: 5,
                        id: 2,
                        limit: 8,//限购
                        pic: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name: "商品3",
                        price: 30,
                        number: 10,
                        id: 3,
                        limit: 15,//限购
                        pic: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name: "商品4",
                        price: 8,
                        number: 6,
                        id: 4,
                        limit: 18,//限购
                        pic: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name: "商品5",
                        price: 14,
                        number: 2,
                        id: 5,
                        limit: 7,//限购
                        pic: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    }
                ]
            },
            methods: {
                allCheck() {
                    if (this.isChecked) {
                        this.checkGroup = this.list.slice()
                    } else {
                        this.checkGroup = []
                    }
                },
                isAllCheck() {
                    if (this.checkGroup.length === this.list.length) {
                        this.isChecked = true
                    } else {
                        this.isChecked = false
                    }
                },
                deleteGood(index, id) {
                    this.list = this.list.filter(val => val.id !== id)
                    this.checkGroup = this.checkGroup.filter(val => val.id !== id)
                    this.isAllCheck()
                },
                totalPrice() {
                    let total = 0
                    this.checkGroup.forEach((value) => {
                        total += value.number * value.price
                    })
                    return total
                }
            },
            computed: {
                computedCheckAll: {
                    get() {
                        return this.checkGroup.length === this.list.length
                    },
                    set(flag) {
                        if (flag) {
                            this.checkGroup = this.list.slice()
                        } else {
                            this.checkGroup = []
                        }
                    }
                }
            }
        })
    </script>
</body>

</html>